<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Image library
 */
class Image {
	private $_file_name = null;
	private $_image = null;
	private $_image_type = null;
	
	/**
	 * Constructor
	 */
	public function __construct($file_name = '') {
		if (!empty($file_name)) {
			$this->load($file_name);
		}
	}
	
	/**
	 * Destructor
	 */
	public function __destruct() {
		$this->destroy();
	}

	/**
	 * Clear object
	 */
	public function clear() {
		$this->_file_name = null;
		$this->_image = null;
		$this->_image_type = null;
	}

	/**
	 * Is file is image
	 */
	public function isImage() {
		if ($this->_image) {
			return true;
		}
		return false;
	}
	
	/**
	 * Loading image
	 */
	public function load($file_name) {
		$this->clear();
		$this->_file_name = $file_name;
		
		$image_info = getimagesize($this->_file_name);
		$this->_image_type = $image_info[2];
		
		if ($this->_image_type == IMAGETYPE_JPEG) {
			$this->_image = imagecreatefromjpeg($this->_file_name);
		} else if ($this->_image_type == IMAGETYPE_GIF) {
			$this->_image = imagecreatefromgif($this->_file_name);
		} else if ($this->_image_type == IMAGETYPE_PNG) {
			$this->_image = imagecreatefrompng($this->_file_name);
		}
	}
	
	/**
	 * Saving image
	 */
	public function save($file_name = '', $compression = 75, $suffix = '') {
		if ($this->_image) {
			if (empty($file_name)) {
				$file_name = $this->_file_name;
			}

			if (empty($compression)) {
				$compression = 75;
			}
			
			if (!empty($suffix)) {
				$file_info = pathinfo($file_name);
				$file_name = $file_info['dirname'] . DIR_SEP;
				$file_name .= $file_info['filename'] . $suffix . '.';
				$file_name .= $file_info['extension'];
				
			}
			
			if ($this->_image_type == IMAGETYPE_JPEG) {
				imagejpeg($this->_image, $file_name, $compression);
			} else if ($this->_image_type == IMAGETYPE_GIF) {
				imagegif($this->_image, $file_name);
			} else if ($this->_image_type == IMAGETYPE_PNG) {
				imagepng($this->_image, $file_name);
			}

		}
		return $file_name;
	}

	/**
	 * Get width of an image
	 */
	public function getWidth() {
		return imagesx($this->_image);
	}
	
	/**
	 * Get height of an image
	 */
	public function getHeight() {
		return imagesy($this->_image);
	}
	
	/**
	 * Resize to width
	 */
	public function resizeToWidth($width) {
		$ratio = $width / $this->getWidth();
		$height = $this->getHeight() * $ratio;
		$this->resize($width, $height);
	}

	/**
	 * Resize to height
	 */
	public function resizeToHeight($height) {
		$ratio = $height / $this->getHeight();
		$width = $this->getWidth() * $ratio;
		$this->resize($width, $height);
	}

	/**
	 * Resize to width or height (by biggest)
	 */
	public function resizeTo($size) {
		if ($this->getWidth() > $this->getHeight()) {
			$this->resizeToWidth($size);
		} else {
			$this->resizeToHeight($size);
		}
	}

	/**
	 * Scale image
	 */
	public function scale($scale) {
		$width = $this->getWidth() * $scale / 100;
		$height = $this->getheight() * $scale / 100;
		$this->resize($width, $height);
	}

	/**
	 * Resizing image
	 */
	public function resize($width, $height) {
		$new_image = imagecreatetruecolor($width, $height);
		imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0,
			$width, $height, $this->getWidth(), $this->getHeight());
		$this->_image = $new_image;
   }
   
   public function destroy() {
		$this->_file_name = null;
		$this->_image_type = null;
		if ($this->_image) {
			imagedestroy($this->_image);
		}
   }
}